package controllers.machine;

import hprose.HproseClinetService;

import java.io.File;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import play.libs.WS;
import play.libs.WS.FileParam;
import play.mvc.Http;
import utils.convert.Convert;
import utils.file.BatchImport;
import utils.file.DownExcel;
import utils.file.ExportExcel;
import utils.logs.LogsUtils;

import com.alibaba.fastjson.JSONObject;
import com.ning.http.util.DateUtil;

import component.UserBis;
import constants.APIConstants;
import constants.Constants;
import controllers.common.filter.BaseFilter;

public class MachineManageAction extends BaseFilter {
	
	
	public static void index(String rsMsg){
		if (StringUtils.isBlank(rsMsg)) {
			render();
		}else{
			render(rsMsg);
		}
	}
	public static void getMachineList(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("currPage", params.get("currPage"));
		map.put("pageSize", params.get("pageSize"));
		map.put("brand", params.get("brand"));
		map.put("model", params.get("model"));
		map.put("machine_code", params.get("machine_code"));
		map.put("isPage", Constants.DATAISPAGE_YES);
		Object[] arguments = new Object[]{map};
		String res = HproseClinetService.synCall(APIConstants.MMI_GETMACHINELIST, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json  = JSONObject.parseObject(res);
		renderJSON(json);
	}
	public static void addMachine(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("machine_code", params.get("machine_code"));
		map.put("brand", params.get("brand"));
		map.put("model", params.get("model"));
		map.put("purpose", params.get("purpose"));
 		Object[] arguments = new Object[]{map};
		LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "添加机台执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
 		String res = HproseClinetService.synCall(APIConstants.MMI_ADDMACHINE, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(res);
		renderJSON(json);
	}
	public static void delMachine(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("machine_id", params.get("machine_id"));
 		Object[] arguments = new Object[]{map};
		LogsUtils.addLogs(Constants.LOG_TYPE_DEL,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "删除机台执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String res = HproseClinetService.synCall(APIConstants.MMI_DELETEMACHINE, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderJSON(res);
	}
	
	public static void upload(File myfile){
		List list = BatchImport.getAllByExcel(myfile); 
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("customer_id", UserBis.currUser());
		paramMap.put("list", list);
		if(list.size() > 1000){
			index("当前Excel行数："+list.size()+";请删除空行,并且行数不超过1000行");
		}
		Object[] arguments = new Object[]{paramMap};
		String res = HproseClinetService.synCall(APIConstants.MMI_ADDMACHINEEXCEL, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(res);
		index(json.get("msg").toString());
	}
	
	public static void download(){
        HSSFWorkbook wb = DownExcel.downExcel(null,"机台品牌:机台型号:机台用途:客户编码",null, null);
        String fileName = "AddMachine.xls";
        try {
            response.setContentTypeIfNotSet("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;filename="
                    + URLEncoder.encode(fileName, "GB2312"));
            OutputStream out = response.out;
            wb.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } 
	}
}
